<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>vue下载网络pdf文件到本地 | 韩志成的个人空间</title><meta name="author" content="韩志成"><meta name="copyright" content="韩志成"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="使用到插件file-saver 使用npm下载 123456789101112131415161718192021222324252627import &amp;#123; saveAs &amp;#125; from &amp;#x27;file-saver&amp;#x27; downloadUrlFile(url, fileName) &amp;#123;      return new Promise((resolve, rej">
<meta property="og:type" content="article">
<meta property="og:title" content="vue下载网络pdf文件到本地">
<meta property="og:url" content="https://hzc-123456.gitee.io/hzcs-blog/2023/05/19/file/index.html">
<meta property="og:site_name" content="韩志成的个人空间">
<meta property="og:description" content="使用到插件file-saver 使用npm下载 123456789101112131415161718192021222324252627import &amp;#123; saveAs &amp;#125; from &amp;#x27;file-saver&amp;#x27; downloadUrlFile(url, fileName) &amp;#123;      return new Promise((resolve, rej">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://hzc-123456.gitee.io/hzcs-blog/img/avar.jpeg">
<meta property="article:published_time" content="2023-05-19T05:58:21.000Z">
<meta property="article:modified_time" content="2023-05-19T08:29:37.491Z">
<meta property="article:author" content="韩志成">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://hzc-123456.gitee.io/hzcs-blog/img/avar.jpeg"><link rel="shortcut icon" href="/hzcs-blog/img/avar.jpeg"><link rel="canonical" href="https://hzc-123456.gitee.io/hzcs-blog/2023/05/19/file/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/hzcs-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/hzcs-blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'vue下载网络pdf文件到本地',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2023-05-19 16:29:37'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/hzcs-blog/img/avar.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/hzcs-blog/archives/"><div class="headline">文章</div><div class="length-num">4</div></a><a href="/hzcs-blog/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/hzcs-blog/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/hzcs-blog/img/back.jpg')"><nav id="nav"><span id="blog-info"><a href="/hzcs-blog/" title="韩志成的个人空间"><span class="site-name">韩志成的个人空间</span></a></span><div id="menus"><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">vue下载网络pdf文件到本地</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-19T05:58:21.000Z" title="发表于 2023-05-19 13:58:21">2023-05-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-19T08:29:37.491Z" title="更新于 2023-05-19 16:29:37">2023-05-19</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hzcs-blog/categories/vue/">vue</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="vue下载网络pdf文件到本地"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>使用到插件file-saver 使用npm下载</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">import &#123; saveAs &#125; from &#x27;file-saver&#x27;</span><br><span class="line"> downloadUrlFile(url, fileName) &#123;</span><br><span class="line">      return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">        let blob = null</span><br><span class="line">        const xhr = new XMLHttpRequest()</span><br><span class="line">        xhr.open(&#x27;GET&#x27;, url)</span><br><span class="line">        xhr.responseType = &#x27;blob&#x27;</span><br><span class="line">        // 加载时处理</span><br><span class="line">        xhr.onload = () =&gt; &#123;</span><br><span class="line">          // 获取返回结果</span><br><span class="line">          blob = xhr.response</span><br><span class="line">          let file = new File([blob], fileName, &#123; type: &#x27;image/png&#x27; &#125;)</span><br><span class="line">          // 返回结果</span><br><span class="line">          resolve(file)</span><br><span class="line">        &#125;</span><br><span class="line">        xhr.onerror = (e) =&gt; &#123;</span><br><span class="line">          reject(e)</span><br><span class="line">        &#125;</span><br><span class="line">        // 发送</span><br><span class="line">        xhr.send()</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">this.downloadUrlFile(url,Date.now()).then((res) =&gt; &#123;</span><br><span class="line">              const name=Date.now()+&#x27;.pdf&#x27;</span><br><span class="line">               saveAs(new Blob([res], &#123; type: &#x27;text/plain;charset=UTF-8&#x27; &#125;),name)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://hzc-123456.gitee.io/hzcs-blog">韩志成</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://hzc-123456.gitee.io/hzcs-blog/2023/05/19/file/">https://hzc-123456.gitee.io/hzcs-blog/2023/05/19/file/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://hzc-123456.gitee.io/hzcs-blog" target="_blank">韩志成的个人空间</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hzcs-blog/tags/%E6%8A%80%E6%9C%AF/">技术</a></div><div class="post_share"><div class="social-share" data-image="/hzcs-blog/img/avar.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/hzcs-blog/2023/05/19/git_order/" title="常用git操作"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">常用git操作</div></div></a></div><div class="next-post pull-right"><a href="/hzcs-blog/2023/05/19/hello-world/" title="第一次发布"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">第一次发布</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/hzcs-blog/2023/05/19/git_order/" title="常用git操作"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-19</div><div class="title">常用git操作</div></div></a></div><div><a href="/hzcs-blog/2023/05/24/VUE3_Composition_API/" title="Vue3 之 Composition API"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-24</div><div class="title">Vue3 之 Composition API</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/hzcs-blog/img/avar.jpeg" onerror="this.onerror=null;this.src='/hzcs-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">韩志成</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/hzcs-blog/archives/"><div class="headline">文章</div><div class="length-num">4</div></a><a href="/hzcs-blog/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/hzcs-blog/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客</div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hzcs-blog/2023/05/24/VUE3_Composition_API/" title="Vue3 之 Composition API">Vue3 之 Composition API</a><time datetime="2023-05-24T02:18:00.000Z" title="发表于 2023-05-24 10:18:00">2023-05-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hzcs-blog/2023/05/19/git_order/" title="常用git操作">常用git操作</a><time datetime="2023-05-19T08:27:19.000Z" title="发表于 2023-05-19 16:27:19">2023-05-19</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hzcs-blog/2023/05/19/file/" title="vue下载网络pdf文件到本地">vue下载网络pdf文件到本地</a><time datetime="2023-05-19T05:58:21.000Z" title="发表于 2023-05-19 13:58:21">2023-05-19</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hzcs-blog/2023/05/19/hello-world/" title="第一次发布">第一次发布</a><time datetime="2023-05-19T05:38:21.000Z" title="发表于 2023-05-19 13:38:21">2023-05-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 韩志成</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hzcs-blog/js/utils.js"></script><script src="/hzcs-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>